package com.erification.web.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.crypto.SecretKey;

import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Controller;

import com.erification.web.dataTransfer.KeyManager;
import com.erification.web.model.Group;
import com.erification.web.service.GroupService;
import com.erification.web.service.TransferService;
import com.erification.web.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller("transferAction")
public class TransferAction extends ActionSupport {
	@Resource
	private KeyManager keyManager;
	@Resource
	private GroupService groupService;
	@Resource 
	private UserService userService;
	@Resource
	private TransferService transferService;
	private String username;
	private String encryptioin;
	private String decryption;
	
	public String getEncryptioin() {
		return encryptioin;
	}
	public void setEncryptioin(String encryptioin) {
		this.encryptioin = encryptioin;
	}
	public String getDecryption() {
		return decryption;
	}
	public void setDecryption(String decryption) {
		this.decryption = decryption;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	@Override
	public String execute(){
		List<Group> list = groupService.getAll(userService.getUser(username).getId());
		Group g = list.get(0);
		String message = "FirstName:" + g.getTrueName().getFirstName() + "\n";
		message = message + "LastName:" + g.getTrueName().getLastName() + "\n";
		message = message + "Celephone:" + g.getCelephone().getCelephone() + "\n";
		message = message + "Address:" + g.getAddress().getAddress() + "\n";
		message = message + "Email:" + g.getEmail().getEmail() + "\n";
		byte[] afterEncrypttion = transferService.encrypt(message,keyManager);
		encryptioin = Base64.encodeBase64String(afterEncrypttion);
		decryption = transferService.decrypt(afterEncrypttion, keyManager);
		return SUCCESS;
	}
	
	
}
